package ciyuan.Gui.Particle;

import com.mojang.blaze3d.vertex.IVertexBuilder;
import net.minecraft.client.Minecraft;
import net.minecraft.client.particle.IParticleRenderType;
import net.minecraft.client.particle.SpriteTexturedParticle;
import net.minecraft.client.renderer.ActiveRenderInfo;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.vector.Quaternion;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.util.math.vector.Vector3f;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;

public class DamageParticle extends SpriteTexturedParticle {

    private int count = 0;
    private final DamageParticleData.DamageType type;

    protected DamageParticle(ClientWorld world, double x, double y, double z, Vector3d speed,int count,DamageParticleData.DamageType type) {
        super(world, x, y, z, speed.x, speed.y, speed.z);
        lifetime = 20;
        xd = speed.x;
        yd = speed.y;
        zd = speed.z;
        setAlpha(1F);
        this.count = count;
        this.type = type;
        //setColor(255,100,3);
        this.hasPhysics = true;
    }

    @Override
    public IParticleRenderType getRenderType() {
        return IParticleRenderType.PARTICLE_SHEET_TRANSLUCENT;
    }

    private static BigDecimal sqrt(BigDecimal value, int scale){
        BigDecimal num2 = BigDecimal.valueOf(2);
        int precision = 100;
        MathContext mc = new MathContext(precision, RoundingMode.HALF_UP);
        BigDecimal deviation = value;
        int cnt = 0;
        while (cnt < precision) {
            deviation = (deviation.add(value.divide(deviation, mc))).divide(num2, mc);
            cnt++;
        }
        deviation = deviation.setScale(scale, BigDecimal.ROUND_HALF_UP);
        return deviation;
    }

    @Override
    public void render(IVertexBuilder iVertexBuilder, ActiveRenderInfo activeRenderInfo, float v) {
        if (Minecraft.getInstance().cameraEntity!=null) {
            String num = String.valueOf(count);
            for (int i=0;i<num.length();i++) {
                Vector3d vector3d = Minecraft.getInstance().cameraEntity.getLookAngle();
                BigDecimal x = new BigDecimal(vector3d.x);
                BigDecimal y = new BigDecimal(vector3d.y);
                BigDecimal z = new BigDecimal(vector3d.z);
                BigDecimal length = x.multiply(x).add(y.multiply(y)).add(z.multiply(z));
                length = sqrt(length,4);
                vector3d = new Vector3d(vector3d.x/length.doubleValue(),vector3d.y/length.doubleValue(),vector3d.z/length.doubleValue());
                vector3d = vector3d.scale((0.15+x.abs().doubleValue()*0.15)*(i+1));
                renderANumber(iVertexBuilder,activeRenderInfo,v,num,i,new Vector3d(-vector3d.z,vector3d.y,vector3d.x));
            }
        }


    }

    private void renderANumber(IVertexBuilder p_225606_1_, ActiveRenderInfo p_225606_2_, float p_225606_3_,String num,int pos,Vector3d vector3f2) {
        Vector3d vector3d = p_225606_2_.getPosition();
        float f = (float) (MathHelper.lerp(p_225606_3_, this.xo, this.x) - vector3d.x());
        float f1 = (float) (MathHelper.lerp(p_225606_3_, this.yo, this.y) - vector3d.y());
        float f2 = (float) (MathHelper.lerp(p_225606_3_, this.zo, this.z) - vector3d.z());
        Quaternion quaternion;
        if (this.roll == 0.0F) {
            quaternion = p_225606_2_.rotation();
        } else {
            quaternion = new Quaternion(p_225606_2_.rotation());
            float f3 = MathHelper.lerp(p_225606_3_, this.oRoll, this.roll);
            quaternion.mul(Vector3f.ZP.rotation(f3));
        }

        Vector3f vector3f1 = new Vector3f(-1.0F, -1.0F, 0.0F);
        vector3f1.transform(quaternion);
        Vector3f[] avector3f = new Vector3f[]{new Vector3f(-1.0F, -1.0F, 0.0F), new Vector3f(-1.0F, 1.0F, 0.0F), new Vector3f(1.0F, 1.0F, 0.0F), new Vector3f(1.0F, -1.0F, 0.0F)};
        float f4 = this.getQuadSize(p_225606_3_)*1.2F;

        for(int i = 0; i < 4; ++i) {
            Vector3f vector3f = avector3f[i];
            vector3f.transform(quaternion);
            vector3f.mul(f4);
            vector3f.add(f, f1, f2);
        }

        float f7 = Integer.parseInt(String.valueOf(num.charAt(pos)))*0.058F;
        float f8 = 0.058F + Integer.parseInt(String.valueOf(num.charAt(pos)))*0.058F;
        float f5 = 0 + 0.058F*2;
        float f6 = 0.058F + 0.058F*2;
        if (this.type == DamageParticleData.DamageType.physicsNormal) {
            f5 = 0 + 0.058F*5;
            f6 = 0.058F + 0.058F*5;
        } else if (this.type == DamageParticleData.DamageType.physicsCrital) {
            f5 = 0 + 0.058F*2;
            f6 = 0.058F + 0.058F*2;
        } else if (this.type == DamageParticleData.DamageType.magicNormal) {
            f5 = 0 + 0.058F*4;
            f6 = 0.058F + 0.058F*4;
        } else if (this.type == DamageParticleData.DamageType.magicCrital) {
            f5 = 0 + 0.058F*3;
            f6 = 0.058F + 0.058F*3;
        }

        int j = this.getLightColor(p_225606_3_);
        p_225606_1_.vertex((double)avector3f[0].x() + vector3f2.x() , (double)avector3f[0].y() +1, (double)avector3f[0].z() + vector3f2.z() ).uv(f8, f6).color(this.rCol, this.gCol, this.bCol, this.alpha).uv2(j).endVertex();
        p_225606_1_.vertex((double)avector3f[1].x() + vector3f2.x(), (double)avector3f[1].y()+1, (double)avector3f[1].z() + vector3f2.z() ).uv(f8, f5).color(this.rCol, this.gCol, this.bCol, this.alpha).uv2(j).endVertex();
        p_225606_1_.vertex((double)avector3f[2].x() + vector3f2.x(), (double)avector3f[2].y()+1, (double)avector3f[2].z() + vector3f2.z() ).uv(f7, f5).color(this.rCol, this.gCol, this.bCol, this.alpha).uv2(j).endVertex();
        p_225606_1_.vertex((double)avector3f[3].x() + vector3f2.x(), (double)avector3f[3].y()+1, (double)avector3f[3].z() + vector3f2.z()).uv(f7, f6).color(this.rCol, this.gCol, this.bCol, this.alpha).uv2(j).endVertex();
    }

}
